package org.apache.rocketmq.example.quickstart;

import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;
import org.apache.rocketmq.remoting.exception.RemotingException;

import java.io.IOException;
import java.util.Date;

/**
 * @author 天启
 * @date 2020-03-18 14:59
 * @description 消息发送DEBUG
 */
public class MyProducer {
    public static void main(String[] args) throws MQClientException, InterruptedException, IOException, RemotingException, MQBrokerException {
        // 初始化消息发送者
        DefaultMQProducer producer = null;
        try {
            // 初始化消息发送者
            producer = new DefaultMQProducer("zhouj_producer_send_debug");

            // 开启故障延迟机制
            producer.setSendLatencyFaultEnable(true);

            producer.setVipChannelEnabled(false);

            // 指定Namesrc
            producer.setNamesrvAddr("127.0.0.1:9876");

            // 开启
            producer.start();

            // 发送消息
            String body = "Hello RocketMq!" + System.currentTimeMillis();
            Message msg = new Message("BI-PUSH-RQ-ZHOUJ", "TagDebug", body.getBytes(RemotingHelper.DEFAULT_CHARSET));
            msg.setDelayTimeLevel(3);
            SendResult sendResult = producer.send(msg);
            System.out.println(new Date());
            System.out.println(sendResult);


        } finally {
            // 关闭消费者
            producer.shutdown();
        }
    }

}
